package com.pan.alg.niuke.tree;



/**
 * 描述
 * 给定一棵二叉树，判断其是否是自身的镜像（即：是否对称）
 * 例如：下面这棵二叉树是对称的
 *
 {1,2,2,3,4,4,3}
 */
public class TreeIsSymmetrical {

    static boolean isSymmetrical(TreeNode pRoot) {

        boolean isSymmetrical = checkIsSymmetrical(pRoot,pRoot);
        return  isSymmetrical;
    }

    private static boolean checkIsSymmetrical(TreeNode pRoot1,TreeNode pRoot2) {
        if(pRoot1==null&&pRoot2==null){
            return true;
        }
        if(pRoot1==null||pRoot2==null||pRoot1.val!=pRoot2.val){
            return false;
        }
        return checkIsSymmetrical(pRoot1.left,pRoot2.right)&&checkIsSymmetrical(pRoot1.right,pRoot2.left);
    }


    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode left = new TreeNode(2);
        TreeNode right = new TreeNode(2);
        root.left=left;
        root.right=right;
        boolean symmetrical = isSymmetrical(root);
        System.out.println(symmetrical);
    }
}
